/**
 * 
 */
package q10;

import java.util.Random;
import java.util.Scanner;

/**
 * @author 11611601 何海彬
 * @date Apr 18, 2017 9:35:25 AM
 */
public class Game {
	final static int hintNow = 2; //hint answer after times of wrong
	
	public static void play() {
		Random randomNum = new Random();
		int num1, num2, num3;
		
		switch(randomNum.nextInt(3) + 1) {
		//switch(3){
		case 1:
			num1 = randomNum.nextInt(9) + 1;
			playGame(num1);
			break;
		case 2:
			num1 = randomNum.nextInt(9) + 1;
			num2 = randomNum.nextInt(9) + 1;
			playGame(num1, num2);
			break;
		case 3:
			num1 = randomNum.nextInt(9) + 1;
			num2 = randomNum.nextInt(9) + 1;
			num3 = randomNum.nextInt(9) + 1;
			playGame(num1, num2, num3);
			break;
		}
	}
	
	//square root
	private static void playGame(int num) {
		Scanner input = new Scanner(System.in);
		
		System.out.println("You have to type the right answer to quit.");
		System.out.printf("What is the square root of %d?\n", num);
		
		double answer = Math.round(Math.sqrt(num) * 100) / 100.0;
		int wrongCount = 0; //count the number of wrong answer
		while(true) {
			double userInput = input.nextDouble();
			input.nextLine();	//clear input buffer.
			
			if(Math.abs(answer - userInput) < 0.00001) {
				//answer is correct
				System.out.println("Congratulations!");
				break;
			} else {
				System.out.println("Answer is not correct. Try again.");
				wrongCount ++;
				if(wrongCount == hintNow) System.out.printf("Hint:%.2f\n", answer);
			}
		}
		
		input.close();
	}
	
	//sum
	private static void playGame(int num1, int num2) {
		Scanner input = new Scanner(System.in);
		
		System.out.println("You have to type the right answer to quit.");
		System.out.printf("What is the sum of %d and %d?\n", num1, num2);
		
		int answer = num1 + num2;
		int wrongCount = 0; //count the number of wrong answer
		while(true) {
			int userInput = input.nextInt();
			input.nextLine();	//clear input buffer.
			
			if(answer == userInput) {
				//answer is correct
				System.out.println("Congratulations!");
				break;
			} else {
				System.out.println("Answer is not correct. Try again.");
				wrongCount ++;
				if(wrongCount == hintNow) System.out.printf("Hint:%d\n", answer);
			}
		}
		
		input.close();
	}
	
	//product
	private static void playGame(int num1, int num2, int num3) {
		Scanner input = new Scanner(System.in);
		
		System.out.println("You have to type the right answer to quit.");
		System.out.printf("What is the product of %d, %d and %d?\n", num1, num2, num3);
		
		int answer = num1 * num2 * num3;
		int wrongCount = 0; //count the number of wrong answer
		while(true) {
			int userInput = input.nextInt();
			input.nextLine();	//clear input buffer.
			
			if(answer == userInput) {
				//answer is correct
				System.out.println("Congratulations!");
				break;
			} else {
				System.out.println("Answer is not correct. Try again.");
				wrongCount ++;
				if(wrongCount == hintNow) System.out.printf("Hint:%d\n", answer);
			}
		}
		
		input.close();
	}
}
